{% extends "base.html" %}
{% block title %}{{lang.menu_links.config.h2}} {{service_desc.service}}{% endblock %}
{% block h2 %}{{lang.menu_links.config.h2}} {{service_desc.service}}{% endblock %}
{% block content %}
<link rel="stylesheet" href="/inc/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="/inc/codemirror/addon/dialog/dialog.css">
<link rel="stylesheet" href="/inc/codemirror/addon/fold/foldgutter.css">
<script src="/inc/codemirror/lib/codemirror.js"></script>
<script src="/inc/codemirror/addon/search/search.js"></script>
<script src="/inc/codemirror/addon/search/searchcursor.js"></script>
<script src="/inc/codemirror/addon/search/jump-to-line.js"></script>
<script src="/inc/codemirror/addon/search/matchesonscrollbar.js"></script>
<script src="/inc/codemirror/addon/search/match-highlighter.js"></script>
<script src="/inc/codemirror/addon/dialog/dialog.js"></script>
<script src="/inc/codemirror/addon/edit/matchbrackets.js"></script>
<script src="/inc/codemirror/addon/edit/closebrackets.js"></script>
<script src="/inc/codemirror/addon/comment/comment.js"></script>
<script src="/inc/codemirror/addon/wrap/hardwrap.js"></script>
<script src="/inc/codemirror/addon/fold/foldcode.js"></script>
<script src="/inc/codemirror/addon/fold/foldgutter.js"></script>
<script src="/inc/codemirror/addon/fold/brace-fold.js"></script>
<script src="/inc/codemirror/addon/fold/comment-fold.js"></script>
<script src="/inc/codemirror/addon/scroll/annotatescrollbar.js"></script>
<script src="/inc/codemirror/mode/nginx.js"></script>
<script src="/inc/codemirror/mode/haproxy.js"></script>
<script src="/inc/codemirror/keymap/sublime.js"></script>
<script src="/app/static/js/configshow.js"></script>
{% if is_serv_protected and g.user_params['role'] > 2 %}
<meta http-equiv="refresh" content="0; url=/app/service">
{% else %}
	{% if g.user_params['servers']|length == 0 %}
		{% include 'include/getstarted.html' %}
	{% else %}
	<p>
		<form action="{{ action }}" method="post" class="left-space">
			<input type="hidden" id="service" value="{{service|default('haproxy', true)}}" />
			{{ select('serv', values=g.user_params['servers'], is_servers='true', selected=serv) }}
			{% if service == 'nginx' or service == 'apache' %}
			<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.show|title()}} {{lang.words.running}} {{lang.words.config}}" onclick="showConfigFiles()">{{lang.words.open|title()}}</a>
			{% else %}
			<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.show|title()}} {{lang.words.running}} {{lang.words.config}}" onclick="showConfig()">{{lang.words.open|title()}}</a>
			{% endif %}
			{% if service != 'keepalived' and service != 'apache' %}
				<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.view|title()}} {{lang.words.stat}}" onclick="openStats()">{{lang.menu_links.stats.link}}</a>
			{% endif %}
			{% if service != 'keepalived' and service != 'nginx' and service != 'apache' %}
				<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.show|title()}} {{lang.words.map}}" onclick="showMap()">{{lang.words.map|title()}}</a>
			{% endif %}
				<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.compare|title()}} {{lang.words.configs}}" onclick="showCompareConfigs()">{{lang.words.compare|title()}}</a>
			{% if g.user_params['role'] <= 3 %}
				<a class="ui-button ui-widget ui-corner-all" title="{{lang.words.show|title()}} {{lang.words.versions}}" onclick="openVersions()">{{lang.menu_links.versions.link}}</a>
			{% endif %}
			{% if g.user_params['role'] <= 2 %}
				<a href="/app/servers#backup" class="ui-button ui-widget ui-corner-all" title="Git">Git</a>
			{% endif %}
		</form>
	</p>
	<div id="ajax-config_file_name"></div>
	{% endif %}

	{% if stderr or error %}
		{% include 'include/errors.html' %}
	{% endif %}

	{% if config %}
		{% if g.user_params['role'] <= 3 %}
			<h4 class="left-space">{{lang.words.config|title()}} {% if config_file_name and config_file_name != 'undefined' %}{{config_file_name.replace('92', '/')}}{%endif%} {{lang.words.from}} {{ serv }}</h4>
			<form action="/app/config/{{service}}/{{serv}}/save" name="saveconfig" id="saveconfig" method="post" class="left-space">
				<input type="hidden" value="{{ serv }}" name="serv">
				<input type="hidden" value="{{ cfg }}.old" name="oldconfig">
				<input type="hidden" value="{{ token }}" name="token">
				<input type="hidden" value="{{ service }}" name="service">
				<input type="hidden" value="{{ config_file_name }}" name="config_file_name">
				<div>
					<textarea name="config" id="config_text_area" class="config" rows="35" cols="100">{{ config }}</textarea>
				</div>
				<p>
					<a href="/app/config/{{service}}/{{serv}}/show" class="ui-button ui-widget ui-corner-all" title="{{lang.phrases.return_to_config}}">{{lang.words.back|title()}}</a>
					<button type="submit" value="test" name="save" class="btn btn-default" title="{{lang.words.check|title()}} {{lang.words.config}} {{lang.words.without}} {{lang.words.saving}}">{{lang.phrases.check_config}}</button>
					<button type="submit" value="save" name="save" class="btn btn-default" title="{{lang.phrases.save_title}}">{{lang.words.save|title()}}</button>
					{% if is_restart|int == 0 %}
					<button type="submit" value="restart" name="" class="btn btn-default">{{lang.phrases.save_and_restart}}</button>
					{% endif %}
					<button type="submit" value="reload" name="save" class="btn btn-default">{{lang.phrases.save_and_reload}}</button>
					{% if service != 'keepalived' %}
					<div class="alert alert-info" style="margin-left: -0px;"><b>{{lang.words.note|title()}}:</b> {{lang.phrases.master_slave}}</div>
					{% endif %}
				</p>
			</form>
		{% endif %}
	{% endif %}
<script>
	var cur_url = window.location.href.split('/app/').pop();
	cur_url = cur_url.split('/');
	if (cur_url[1] == 'map') {
		showMap();
	}
	if (cur_url[1] == 'compare') {
		showCompareConfigs();
	}
	if (cur_url[3] == 'show') {
		showConfig();
	}
	if (cur_url[3] == 'show-files') {
		showConfigFiles();
	}
	if (cur_url[3] == 'findInConfig') {
		var words = findGetParameter('findInConfig');
		waitForElm('#finding_words_from').then((elm) => {
			$('#find_p').show();
			$('#words').val(words);
			findInConfig(words);
		});
	}
	if (cur_url[1] == 'config_file_name') {
		showConfigFilesForEditing();
	}
	if (cur_url[1] == 'haproxy' && cur_url[3] == 'edit') {
		var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("config_text_area"),
				{
					mode: "haproxy",
					lineNumbers: true,
					lineWrapping: true,
					autocapitalize: true,
					autocorrect: true,
					spellcheck: true,
					autoCloseBrackets: true,
					keyMap: "sublime",
					matchBrackets: true,
					foldGutter: true,
					showCursorWhenSelecting: true,
    				gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "breakpoints"],
					highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: true}
				});
	} else {
		var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("config_text_area"),
				{
					mode: "nginx",
					lineNumbers: true,
					lineWrapping: true,
					autocapitalize: true,
					autocorrect: true,
					spellcheck: true,
					autoCloseBrackets: true,
					keyMap: "sublime",
					matchBrackets: true,
					foldGutter: true,
					showCursorWhenSelecting: true,
    				gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "breakpoints"],
					highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: true}
				});
	}
	myCodeMirror.on("gutterClick", function(cm, n) {
		var info = cm.lineInfo(n);
		cm.setGutterMarker(n, "breakpoints", info.gutterMarkers ? null : makeMarker());
	});
	myCodeMirror.on("beforeChange", function (cm, change) {
		$(window).bind('beforeunload', function(){
		  return 'Are you sure you want to leave?';
		});
	});
	function makeMarker() {
		var marker = document.createElement("div");
		marker.style.color = "#822";
		marker.innerHTML = "●";
		return marker;
	}
</script>
<style>
.CodeMirror {
  	line-height: 1.2em;
  	height: 70%;
}
</style>
<script>
	myCodeMirror.refresh();
	$(document).bind('keydown', 'ctrl+s', function (){
		$("[type='submit'][value='save']").click();
	});
	$(document).bind('keydown', 'ctrl+d', function (){
		$("[type='submit'][value='test']").click();
	});
	$(document).bind('keydown', 'ctrl+e', function (){
		$("[type='submit'][value='reload']").click();
	});
	{% if is_restart|int == 0 %}
	$(document).bind('keydown', 'ctrl+r', function (){
		$("[type='submit'][value='restart']").click();
	});
	{% endif %}
</script>
{% endif %}
{% endblock %}
